<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * php  SHA 256  算法
 */
function sha256($data, $rawOutput=false){
    if(!is_scalar($data)){
        return false;
    }
    $data = (string)$data;
    $rawOutput = !!$rawOutput;
    return hash('sha256', $data, $rawOutput);
}



/**
 * Get 请求 模拟登陆
 * @param  string $url 访问链接
 * @return mix
 */
function curlGet($url)
{
    // 使用 curl_init()函数初始化一个CURL会话，初始化成功后返回一个句柄供curl_setopt(), curl_exec(),和 curl_close() 函数使用
    $curl = curl_init();
    // 使用curl_setopt()设置要获取的URL地址
    curl_setopt($curl, CURLOPT_URL, $url);
    // 设置是否输出header 设为0 否则影响输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 设置是否输出结果
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 设置是否输出结果
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 设置是否检查服务器端的证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // 使用curl_exec()将CURL返回的结果转换成正常数据并保存到一个变量
    $data = curl_exec($curl);
    // 使用 curl_close() 关闭CURL会话
    curl_close($curl);
    return $data;
}


//gei请求curl
function curl_get($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    return  $data;
}



/**
 * url:访问路径
 * array:要传递的数组
 */
function curl_post($url,$array){
    $curl = curl_init();
    //设置提交的url
    curl_setopt($curl, CURLOPT_URL, $url);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    $post_data = $array;
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);


    //执行命令
    $data = curl_exec($curl);

    //关闭URL请求
    curl_close($curl);
    //获得数据并返回
    return $data;
}

/**
 * 多个数组
 *
 * @param unknown_type $data
 */
function combineDika() {
    $data = func_get_args();
    $data = current($data);
    $cnt = count($data);
    $result = array();
    $arr1 = array_shift($data);
    foreach($arr1 as $key=>$item)
    {
        $result[] = array($item);
    }

    foreach($data as $key=>$item)
    {
        $result = combineArray($result,$item);
    }
    return $result;
}


function array_diff1($array_1, $array_2) {
    $array_2 = array_flip($array_2);
    foreach ($array_1 as $key => $item) {
        if (isset($array_2[$item])) {
            unset($array_1[$key]);
        }
    }
    return $array_1;
}

if (!function_exists('get_client_ip')) {
    /**
     * 获取客户端IP地址
     * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @param bool $adv 是否进行高级模式获取（有可能被伪装）
     * @return mixed
     */
    function get_client_ip($type = 0, $adv = false) {
        $type       =  $type ? 1 : 0;
        static $ip  =   NULL;
        if ($ip !== NULL) return $ip[$type];
        if($adv){
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos    =   array_search('unknown',$arr);
                if(false !== $pos) unset($arr[$pos]);
                $ip     =   trim($arr[0]);
            }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip     =   $_SERVER['HTTP_CLIENT_IP'];
            }elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip     =   $_SERVER['REMOTE_ADDR'];
            }
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u",ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }
}

/**
 * 生成二维码
 * @return $outfile 图片路径名称
 */
function qrcode($access,$sn,$type = 0){
    $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$access";
    if($type==0){
        $param = json_encode(array("path"=>"pages/index/index?id=$sn","width"=> 150));
    }elseif ($type==1){
        $param = json_encode(array("path"=>"pages/bindVip/bindVip?offline_id=$sn","width"=> 150));
    }elseif($type== 2){
        $param = json_encode(array("path"=>"pages/index/index?user_id=$sn","width"=> 150));
    }

    $result = httpRequest($qcode, $param,"POST");
    if($type == 2){
        file_put_contents("uploads/qrcode/distribution/$sn.png", $result);
    }elseif($type == 1){
        file_put_contents("uploads/qrcode/offline/$sn.png", $result);
    }else{
        file_put_contents("uploads/qrcode/$sn.png", $result);
    }

}

    //把请求发送到微信服务器换取二维码
function httpRequest($url, $data='', $method='GET'){
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
      if($method=='POST')
      {
          curl_setopt($curl, CURLOPT_POST, 1);
          if ($data != '')
          {
              curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
          }
      }

      curl_setopt($curl, CURLOPT_TIMEOUT, 30);
      curl_setopt($curl, CURLOPT_HEADER, 0);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
      $result = curl_exec($curl);
      curl_close($curl);
      return $result;
}



//发送提示
function getMessage($account,$password,$phone,$msg){
    $data['account'] = $account;
    $data['pswd'] = $password;
    $data['msg'] = urlencode($msg);
    $data['mobile'] = $phone;
    $data['needstatus'] = 'false';
    $data['product'] = '';
    $data['extno'] = '';

    $result = curlPost($data);
    if ($result) {
        return 1;
    } else {
        return 2;
    }


}


//发送短信用到
function curlPost($array){
    $curl = curl_init();
    $header = array(
        "Content-Type:application/x-www-form-urlencoded",
        "Accept:text/plain"
    );
    //设置提交的url
    $url = "http://222.73.117.158/msg/HttpBatchSendSM";
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    //设置头文件的信息作为数据流输出
    curl_setopt($curl, CURLOPT_HEADER, 0);
    //设置获取的信息以文件流的形式返回，而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_POST, 1);
    //设置post数据
    $post_data = http_build_query($array);

    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

    //执行命令
    $data = curl_exec($curl);

    //关闭URL请求
    curl_close($curl);
    //获得数据并返回
    return $data;
}

//生成订单编号
function generateOrderNum(){
    $mtimestamp = sprintf("%.3f", microtime(true)); // 带毫秒的时间戳
    $timestamp = floor($mtimestamp); // 时间戳
    $milliseconds = round(($mtimestamp - $timestamp) * 1000); // 毫秒
    $order_sn = date("ymdHis", $timestamp).sprintf("%03d",$milliseconds).mt_rand(100,999);
    return $order_sn;
}

//查询是否包含p标签
function judgeHtml($str){
    if(strpos($str,'<p>')!==false){
        return $str;
    }else{
        return '<p>'.$str.'</p>';
    }

}